<?php

namespace app\api\controller;

use app\api\model\User as UserModel;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login()
    {
        //$str = var_export($this->request->post(),TRUE);
        //file_put_contents('123.txt',$str);
        $model = new UserModel;

        return $this->renderSuccess([
            'user_id' => $model->login($this->request->post()),
            'token' => $model->getToken()
        ]);
    }

    public function getUserMobile(){
        $userInfo = $this->getUser();
        $post['code']=input('request.code');
        $post['encryptedData'] = input('request.encryptedData');
        $post['iv'] = input('request.iv');
        if(!$post['code'] || !$post['encryptedData'] || !$post['iv']){
            return $this->renderError('参数不完整');
        }
        $model = new UserModel;
        $mobile=$model->getMobile($post);
        $saveData['mobile']=$mobile;
        $saveData['user_id']=$userInfo->user_id;
        $res=UserModel::update($saveData);
        if($res){
            return $this->renderSuccess([], '手机号保存成功');
        } else {
            return $this->renderError('手机号保存失败');
        }
    }

    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        return $this->renderSuccess(compact('userInfo'));
    }

}
